import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/DemoPages/Map/show_map_type_page.dart';
import 'package:flutter_bmfmap_example/http/http.dart';
import 'package:flutter_bmfmap_example/models/collectioninfomodel.dart';
import 'package:flutter_bmfmap_example/models/deviceinfomodel.dart';
import 'package:flutter_bmfmap_example/models/familyinfomodel.dart';
import 'package:flutter_bmfmap_example/models/marketinfomodel.dart';
import 'package:flutter_bmfmap_example/models/orderinfomodel.dart';
import 'package:flutter_bmfmap_example/models/userinformodel.dart';
import 'package:flutter_bmfmap_example/pages/homepage/homepage.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/marketpage.dart';
import 'package:flutter_bmfmap_example/pages/positionpage/positionpage.dart';
import 'package:flutter_bmfmap_example/service/marketservice.dart';

import 'package:flutter_svg/svg.dart';
import 'package:proste_indexed_stack/proste_indexed_stack.dart';
import 'package:provider/provider.dart';

import 'minepage/minepage.dart';

class RootPage extends StatefulWidget {
  const RootPage({Key key}) : super(key: key);

  @override
  _RootPageState createState() => _RootPageState();
}

class _RootPageState extends State<RootPage>
    with SingleTickerProviderStateMixin {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  List<IndexedStackChild> _pages;

  int _currentindex = 0;

  // 初始opacityLevel为1.0为可见状态，为0.0时不可见
  double opacityLevel = 1.0;

  @override
  void initState() {
    super.initState();
    _getmarketinfo();
    _pages = [
      // IndexedStackChild(
      //     child: HomePage(
      //   opendrawer: _opendraw,
      // )),
      // IndexedStackChild(child: PositionPage()),
      // IndexedStackChild(child: MarketPage()),
      IndexedStackChild(child: HomePage()),
      IndexedStackChild(child: PositionPage()),
      IndexedStackChild(child: MarketPage()),
    ];

    
  }

  _getmarketinfo() async {
    await context.read<Orderinfomodel>().init();
    await context.read<Shoppingcatmodel>().init();
    await context.read<Collectioninfomodel>().init();
    await context.read<FamilyInfomodel>().init();
    await context.read<DeviceInfomodel>().init();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      drawer: MinePage(),
      body: ConstrainedBox(
        constraints: BoxConstraints.expand(),
        child: Stack(
          alignment: Alignment.center, //指定未定位或部分定位widget的对齐方式
          children: <Widget>[
            Listener(
              child: ProsteIndexedStack(
                index: _currentindex,
                children: _pages,
              ),
              onPointerMove: (event) {
                if (mounted) onPress(0);
                // var position = event.position.distance;
                // //判断距离差
                // var detal = position - _lastMoveY;
                // if (detal > 0) {
                //   //手指移动的距离
                //   double pos = (position - _downY);
                //   print("================向下移动================");
                // } else {
                //   // 所摸点长度 +滑动距离  = IistView的长度  说明到达底部
                //   print("================向上移动================");
                // }
                // _lastMoveY = position;
              },
              onPointerUp: (event) {
                if (mounted) onPress(1);
              },
              onPointerDown: (PointerDownEvent event) {
                // print('down');
              },
            ),
            Positioned(
              bottom: 36.0,
              child: AnimatedOpacity(
                // 使用一个AnimatedOpacity Widget
                opacity: opacityLevel,
                duration: new Duration(milliseconds: 200),
                child: _buildmenubar(),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void onPress(int i) {
    setState(() => opacityLevel = i.toDouble());
  }

  _buildmenubar() {
    return Stack(
      alignment: Alignment.center, //指定未定位或部分定位widget的对齐方式
      children: <Widget>[
        Container(
            decoration: new BoxDecoration(
                color: Colors.white,
                borderRadius: new BorderRadius.circular((24.0)),
                boxShadow: [
                  BoxShadow(
                      color: Colors.black12,
                      offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                      blurRadius: 5.0, //阴影模糊程度
                      spreadRadius: 0.1 //阴影扩散程度
                      )
                ]),
            height: 48,
            width: 296),
        Container(
          decoration: new BoxDecoration(
              color: Colors.white,
              borderRadius: new BorderRadius.circular((32.0)),
              boxShadow: [
                BoxShadow(
                    color: Colors.black12,
                    offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                    blurRadius: 5.0, //阴影模糊程度
                    spreadRadius: 0.1 //阴影扩散程度
                    )
              ]),
          height: 64,
          width: 64,
        ),
        Container(
          height: 48,
          width: 296,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              GestureDetector(
                onTap: () {
                  _currentindex = 0;
                  setState(() {});
                },
                child: SvgPicture.asset(
                  "assets/images/icons/svg/home2.svg",
                  color: _currentindex == 0
                      ? Color.fromRGBO(255, 119, 99, 1)
                      : Colors.black12,
                  width: 32,
                ),
              ),
              GestureDetector(
                onTap: () {
                  _currentindex = 1;
                  setState(() {});
                },
                child: SvgPicture.asset(
                  "assets/images/icons/svg/position.svg",
                  color: _currentindex == 1
                      ? Color.fromRGBO(255, 119, 99, 1)
                      : Colors.black12,
                  width: 40,
                ),
              ),
              GestureDetector(
                onTap: () {
                  _currentindex = 2;
                  setState(() {});
                },
                child: SvgPicture.asset(
                  "assets/images/icons/svg/product.svg",
                  color: _currentindex == 2
                      ? Color.fromRGBO(255, 119, 99, 1)
                      : Colors.black12,
                  width: 32,
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  _opendraw() {
    _scaffoldKey.currentState.openDrawer();
  }
}
